package org.example.huadianguanli.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.example.base.common.model.TSystemDictItemModel;
import org.example.base.common.service.TsSystemDictService;
import org.example.core.constanct.CommonUtils;
import org.example.core.mysql.Where;
import org.example.core.service.impl.SimpleServiceImpl;
import org.example.huadianguanli.entity.TShopCardEntity;
import org.example.huadianguanli.model.TShopCardDetailModel;
import org.example.huadianguanli.model.TShopCardModel;
import org.example.huadianguanli.model.TShopModel;
import org.example.huadianguanli.service.TShopCardDetailService;
import org.example.huadianguanli.service.TShopCardService;
import org.example.huadianguanli.service.TShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2024/2/14 20:19
 */
@Service
@Primary
public class TShopCardServiceImpl extends SimpleServiceImpl<String, TShopCardModel, TShopCardEntity> implements TShopCardService {

    @Autowired
    private TShopCardDetailService tShopCardDetailService;

    @Autowired
    private TShopService tShopService;

    @Autowired
    private TsSystemDictService tsSystemDictService;

    @Override
    public void addShopCard(Map<String, Object> params) {
        String userId = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        Object shopId = params.get("shopId");
        Object count = params.get("count");
        String id = null;

        //查询该用户是否已经添加过购物车
        TShopCardModel tShopCardModel = searchOne(Where.build().field("user_id").equal(userId).and().field("del_flag").equal(0));
        if (ObjectUtils.isEmpty(tShopCardModel)) {
            id = CommonUtils.getUUID().replaceAll("-", "");
            TShopCardModel shopCardModel = new TShopCardModel();
            shopCardModel.setUserId(userId);
            shopCardModel.setId(id);
            add(shopCardModel);
        } else {
            id = tShopCardModel.getId();
        }
        TShopCardDetailModel shopCardDetailModel = new TShopCardDetailModel();
        shopCardDetailModel.setShopCardId(id);
        shopCardDetailModel.setShopId((String) shopId);
        shopCardDetailModel.setCount((Integer) count);

        tShopCardDetailService.add(shopCardDetailModel);
    }

    @Override
    public void updateShopCard(Map<String, Object> params) {
        Object shopCardId = params.get("shopCardId");
        Object shopCardListObject = params.get("shopCardList");
        List<TShopCardDetailModel> shopCardDetailModelList = tShopCardDetailService.search(Where.build().field("shop_card_id").equal(shopCardId).
                and().field("del_flag").equal(0));
        //删除之前的
        shopCardDetailModelList.stream().forEach(tShopCardDetailModel -> {
            tShopCardDetailModel.setDelFlag("1");
            tShopCardDetailService.update(tShopCardDetailModel);
        });
        //新增现在的
        List<Map<String, Object>> shopCardList = (List<Map<String, Object>>) shopCardListObject;
        for (Map<String, Object> shopCardMap : shopCardList) {
            Object shopId = shopCardMap.get("shopId");
            Object count = shopCardMap.get("count");
            TShopCardDetailModel shopCardDetailModel = new TShopCardDetailModel();
            shopCardDetailModel.setCount((Integer) count);
            shopCardDetailModel.setShopId((String) shopId);
            shopCardDetailModel.setShopCardId((String) shopCardId);
            tShopCardDetailService.add(shopCardDetailModel);
        }
    }

    @Override
    public JSONObject getShopCardList(String userId) {
        TShopCardModel shopCardModel = searchOne(Where.build().field("user_id").equal(userId).and().field("del_flag").equal(0));
        if (ObjectUtils.isEmpty(shopCardModel)) {
            return new JSONObject();
        }

        List<TSystemDictItemModel> shopTypeDictModelList = tsSystemDictService.getByCode("shop_type");
        Map<String, String> shopTypeMap = shopTypeDictModelList.stream().collect(Collectors.toMap(TSystemDictItemModel::getCode, TSystemDictItemModel::getName));

        JSONObject shopCardJsonObject = JSONUtil.parseObj(shopCardModel);
        List<TShopCardDetailModel> shopCardDetailModelList = tShopCardDetailService.search(Where.build().field("shop_card_id").equal(shopCardModel.getId()).and().field("del_flag").equal(0));
        List<JSONObject> shopCardDetailList = shopCardDetailModelList.stream().map(shopCardDetailModel -> {
            JSONObject shopCardDetailJsonObject = JSONUtil.parseObj(shopCardDetailModel);
            Object shopId = shopCardDetailJsonObject.get("shopId");
            TShopModel shopModel = tShopService.getById((String) shopId);
            JSONObject shopJSONObject = JSONUtil.parseObj(shopModel);
            shopJSONObject.set("typeStr", shopTypeMap.get(shopModel.getType()));
            shopJSONObject.set("shopDiscountPrice", BigDecimal.valueOf(shopModel.getPrice() * shopModel.getDiscount()).setScale(2,BigDecimal.ROUND_CEILING));
            shopCardDetailJsonObject.set("shop", shopJSONObject);
            return shopCardDetailJsonObject;
        }).collect(Collectors.toList());
        shopCardJsonObject.set("shopCardList", shopCardDetailList);
        return shopCardJsonObject;
    }
}
